package com.henu.engineer.controller.admin

import com.henu.engineer.StringUtils
import com.henu.engineer.service.admin.AdminService
import com.henu.engineer.service.UserService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.PatchMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController

/**
 * 主要是用户信息的管理
 * @author noexcept
 * @since 2021/8/13 0:58
 */
@RestController
@RequestMapping("/ad")
class UserInfoController {

    @Autowired
    lateinit var adminService: AdminService

    @Autowired
    lateinit var userService: UserService

    /**
     * 更新用户身份（除更新为管理员）
     */
    @PatchMapping("/updateTcRank")
    fun teacherRank(teacherId: String?, rank: String?): String? {
        if (StringUtils.isEmpty(teacherId, rank)) {
            return "INFO_INCOMPLETE"
        }
        if (StringUtils.isValidRankElseAd(rank)) {
            return "RANK_ERROR"
        }
        return if (adminService.updateRank(teacherId!!, rank!!)) "SUCCESS" else "FAILED"
    }

    /**
     * 更新学生的信息的特定项
     * 修改特定项的信息 item只能是：name,gender,specialization,contact,identity之一， 并符合相应的数据约束
     */
    @PatchMapping("/st")
    fun updateItem(studentId: String?, item: String?, newValue: String?): String? {
        if (StringUtils.isEmpty(studentId, item, newValue)) {
            return "INFO_INCOMPLETE"
        }
        return if (userService.updateStudentInfo(studentId!!, item!!, newValue!!)) "SUCCESS" else "FAILED"
    }

}